package info.javafans.servlet;

import info.javafans.dao.CategoryDao;
import info.javafans.dao.CommentDao;
import info.javafans.dao.PostDao;
import info.javafans.domain.Category;
import info.javafans.domain.Comment;
import info.javafans.domain.Post;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowSinglePost
 */
public class ShowSinglePost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowSinglePost() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String id = request.getParameter("id");
		
		//增加浏览次数
		PostDao postDao = new PostDao();
		postDao.doViewCountUp(id);
		
		//获得文章
		Post post = postDao.selectPostById(id);
		request.setAttribute("post", post);
		
		//获得该文章的评论列表
		CommentDao commentDao = new CommentDao();
		List<Comment> commentList = commentDao.selectCommentsOfPost(id);
		request.setAttribute("commentList", commentList);
		
		//获得文章分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		request.getRequestDispatcher("single.jsp").forward(request, response);
	}

}
